package com.ljs.shardingspheredemo.config.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

public class MyDatabsePreciseAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        String logicTableName = preciseShardingValue.getLogicTableName();
        String userId=preciseShardingValue.getColumnName();
        Long value = preciseShardingValue.getValue();
        //实现  m$->{user_id%2+1}
        //转化为int类型才能取模
        BigInteger bigInteger=BigInteger.valueOf(value);
        BigInteger bb=bigInteger.mod(new BigInteger("2")).add(new BigInteger("1"));//user_id%2+1
        String key="m"+bb;//m1,m2
        //判断集合中有没有
        if(collection.contains(key)){
            return  key;

        }
        throw new UnsupportedOperationException("请检查配置");


    }
}
